package com.eportal.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.log4j.Logger;

public class DateTool {
	
	private static Logger log = Logger.getLogger(DateTool.class);

	 public static boolean isSameDate(Date date1, Date date2) {
	        Calendar cal1 = Calendar.getInstance();
	        cal1.setTime(date1);

	        Calendar cal2 = Calendar.getInstance();
	        cal2.setTime(date2);

	        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
	                .get(Calendar.YEAR);
	        boolean isSameMonth = isSameYear
	                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
	        boolean isSameDate = isSameMonth
	                && cal1.get(Calendar.DAY_OF_MONTH) == cal2
	                        .get(Calendar.DAY_OF_MONTH);

	        return isSameDate;
	    }
	 public static Date dateFormat(Date d,String ft){
		 SimpleDateFormat format = new SimpleDateFormat(ft);   
		 String dd =format.format(d);    
		 Date ddd;  
		 try {   
			 ddd = format.parse(dd);   
			 return  ddd;
			 } catch (ParseException e) {
				 // TODO Auto-generated catch block   
				 log.error("error",e);
				 return d;
				 }
		 }
		 
		 
	 
	 
	 
	/**
	 * 计算日期的计算月，i为负数表示上i个月，i为正数表示下i个月
	 * @param date
	 * @param i
	 * @return
	 */
	public static Date nextMoth(Date date,int i){
		Calendar calender = Calendar.getInstance();
        calender.setTime(date);
        calender.add(Calendar.MONTH, i);
        return calender.getTime();
	}

	/**
	* 日期转换成字符串
	* @param date 
	* @return str
	*/
	public static String DateToStr(Date date) {
	  
	   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	   String str = format.format(date);
	   return str;
	} 
	/**
	* 日期转换成字符串
	* @param date 
	* @return str
	*/
	public static String DateToStr(Date date,String format_) {
	  
	   SimpleDateFormat format = new SimpleDateFormat(format_);
	   String str = format.format(date);
	   return str;
	} 

	/**
	* 字符串转换成日期
	* @param str
	* @return date
	*/
	public static Date StrToDate(String str) {
	  
	   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	   Date date = null;
	   try {
	    date = format.parse(str);
	   } catch (ParseException e) {
		   log.error("error",e);
	   }
	   return date;
	}
	/**
	* 字符串转换成日期
	* @param str
	* @return date
	*/
	public static Date StrToDate(String str,String format_) {
	  
	   SimpleDateFormat format = new SimpleDateFormat(format_);
	   Date date = null;
	   try {
	    date = format.parse(str);
	   } catch (ParseException e) {
		   log.error("error",e);
	   }
	   return date;
	}
	/**
	* 生成特定月的某一日期
	* @param str
	* @return date
	*/
	public static Date datedayl(Date date,int str) {
		String d=DateToStr(date);		
	   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	   try {
		   if(str!=0){
			   date = format.parse(d.substring(0,8)+str);
		   }	    
	   } catch (ParseException e) {
		   log.error("error",e);
	   }
	   return date;
	}
	/**
	 * 获取日期相日数
	 * @param now
	 * @param returnDate
	 * @return
	 */
	public static int daysBetween(Date now, Date returnDate){
	    Calendar cNow = Calendar.getInstance();
	    Calendar cReturnDate = Calendar.getInstance();
	    cNow.setTime(now);
	    cReturnDate.setTime(returnDate);
	    setTimeToMidnight(cNow);
	    setTimeToMidnight(cReturnDate);
	    long todayMs = cNow.getTimeInMillis();
	    long returnMs = cReturnDate.getTimeInMillis();
	    long intervalMs = todayMs - returnMs;
	    return millisecondsToDays(intervalMs);
	  }

	public static int millisecondsToDays(long intervalMs){
	    return (int) (intervalMs / (1000 * 86400));
	  }

	public static void setTimeToMidnight(Calendar calendar){
	    calendar.set(Calendar.HOUR_OF_DAY, 0);
	    calendar.set(Calendar.MINUTE, 0);
	    calendar.set(Calendar.SECOND, 0);
	  }


	 /**
	　　* 获取日期相月数
	　　* @param 
	　　* @return 日期类型时间
	　　* @throws ParseException 
	　　*/
	public static int getDiffMonth(Date beginDate,Date endDate){
		Calendar calbegin = Calendar.getInstance();
		Calendar calend = Calendar.getInstance();
		calbegin.setTime(beginDate);
		calend.setTime(endDate);
		int m_begin = calbegin.get(Calendar.MONTH)+1; //获得合同开始日期月份
		int m_end = calend.get(Calendar.MONTH)+1;
		//获得合同结束日期月份
		int checkmonth = m_end-m_begin+(calend.get(Calendar.YEAR)-calbegin.get(Calendar.YEAR))*12;
		//获得合同结束日期于开始的相差月份
		return checkmonth;

	}
	/**
	 * 获取日期中的“日”
	 * @param date
	 * @return
	 */
	public static int getDay(Date date){
		Calendar calendar = GregorianCalendar.getInstance(); 
		calendar.setTime(date); 
		return calendar.get(Calendar.DATE);
	}
	/**
	 * 计算日期的计算天，i为负数表示上i天，i为正数表示下i天
	 * @param date
	 * @param i
	 * @return
	 */
	public static Date printNextTime(Date date,int i) 
    { 
        Calendar cal = Calendar.getInstance(); 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
        try 
        { 
            cal.setTime(date); 
            cal.add(Calendar.DATE, i);
            return StrToDate(sdf.format(cal.getTime()));
        } 
        catch (Exception e) 
        { 
            // TODO Auto-generated catch block 
            log.error("error",e);
        }
		return date; 
    } 
  /**
   * 判断d1-d2是否在d3-d4中间
   * 判断d3-d4是否在d1-d2中间
   * @param d1
   * @param d2
   * @param d3
   * @param d4
   * @return 是
   */
	 public static boolean  comTwoDate(Date d1,Date d2,Date d3,Date d4){
		if (d1.compareTo(d3) < 0 && d2.compareTo(d3) < 0) {
			return false;
		} else if (d1.compareTo(d4) > 0 && d2.compareTo(d4) > 0) {
			return false;
		} else if (d1.compareTo(d3) < 0 && d2.compareTo(d3) < 0) {
			return false;
		} 
		else return !(d1 == d3 || d1 == d4 || d2 == d3 || d2 == d4);
	}
	 
	 public static void main(String[] args){
		 int ii=daysBetween(StrToDate("2012-07-25"), StrToDate("2012-08-02"));
		 System.out.println("<<<<<<<<<<<<<<<<<<<<"+ii);
	 }
	 /**
	  * Timestamp -> Date
	 * @return 

	  */
	 public static Date TimestampToDate (Timestamp ts){
		 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				String str = df.format(ts);
				return StrToDate(str);
			} catch (Exception e) {
				log.error("error",e);
			}
			return null;
	 }
	
}
